package com.zzk.protect.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zzk.protect.common.CommonResult;
import com.zzk.protect.pojo.ExaminationApplication;
import com.zzk.protect.pojo.Registration;
import com.zzk.protect.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/registration")
public class RegistrationController {
    @Autowired
    private RegistrationService registrationService;
    @PostMapping("/add")
    public CommonResult add(@RequestBody Registration registration){
        registration.setRegistrationTime(new Date());
        registration.setVisitStatus(1);
        return CommonResult.success(registrationService.save(registration));
    }
    @GetMapping("/checkCaseNumber")
    public CommonResult<Registration> check(@RequestParam String caseNumber){
        QueryWrapper<Registration> qw = new QueryWrapper<>();
        qw.eq("case_number",caseNumber);
        return CommonResult.success(registrationService.getOne(qw));
    }
    @GetMapping("/getByDept")
    public CommonResult<List<Registration>> getByDept(@RequestParam Integer deptId,@RequestParam Integer visitStatus){
        QueryWrapper<Registration> qw = new QueryWrapper<>();
        qw.eq("department_id",deptId);
        qw.eq("visit_status",visitStatus);
        return CommonResult.success(registrationService.list(qw));
    }
    @GetMapping("/getByDoctorId")
    public CommonResult<List<Registration>> getByDoctorId(@RequestParam Integer doctorId,@RequestParam Integer visitStatus){
        QueryWrapper<Registration> qw = new QueryWrapper<>();
        qw.eq("doctor_id",doctorId);
        qw.eq("visit_status",visitStatus);
        return CommonResult.success(registrationService.list(qw));
    }
    @GetMapping("/getByStatus")
    public CommonResult<List<Registration>> getByStatus(@RequestParam Integer visitStatus){
        QueryWrapper<Registration> qw = new QueryWrapper<>();
        qw.eq("visit_status",visitStatus);
        return CommonResult.success(registrationService.list(qw));
    }
    @PostMapping("/updateStatus")
    public CommonResult updateStatus(@RequestBody Registration registration){
        return CommonResult.success(registrationService.updateById(registration));
    }
    @PostMapping("/status")
    public CommonResult status(@RequestParam Integer registrationId, @RequestParam Integer status) {
        try {

            UpdateWrapper<Registration> updateWrapper = new UpdateWrapper<>();


            updateWrapper.eq("id", registrationId);

            // 创建要更新的实体对象，设置status字段
            Registration updateEntity = new Registration();
            updateEntity.setVisitStatus(status);
            boolean success = registrationService.update(updateEntity, updateWrapper);

            if (success) {
                return CommonResult.success("状态更新成功");
            } else {
                return CommonResult.failed("更新失败，未找到匹配的记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("更新状态时发生错误：" + e.getMessage());
        }
    }
}

